package com.aoip.telemetry.telemetry.quartz.task.call;

import com.alibaba.fastjson.JSONObject;
import com.aoip.telemetry.telemetry.configureui.common.utils.http.HttpUtils;
import com.aoip.telemetry.telemetry.core.call.GenerateOrder;
import com.aoip.telemetry.telemetry.core.constant.HttpStatus;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.equipment.QuerySetOrderEntity;
import com.aoip.telemetry.telemetry.entity.order.OrderBaseEntity;
import com.aoip.telemetry.telemetry.entity.order.OrderRecordEntity;
import com.aoip.telemetry.telemetry.entity.order.RelOrderElementEntity;
import com.aoip.telemetry.telemetry.entity.order.RelOrderStEntity;
import com.aoip.telemetry.telemetry.quartz.task.call.mapper.TimeSynCallTaskMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 测站对时自动定时任务
 *
 * @author chenbai
 * @version 1.0
 * @date 2023/5/2 15:39
 */
@Component
@Slf4j
public class TimeSynCallTask {

    @Resource
    private TimeSynCallTaskMapper timeSynCallTaskMapper;
    @Value("${receive.url.address}")
    private String address;
    /**
     * 召测指令ID
     **/
    private static final String TIME_SYN_ORDER_ID = "1549763859219816449";
    private static final String CALL_URL = "/v1/POST/order/call";
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    @Resource
    private GenerateOrder generateOrder;

    public void execute() {
        OrderBaseEntity orderBaseEntity = timeSynCallTaskMapper.getOrderBaseByOrderId(TIME_SYN_ORDER_ID);
        if (orderBaseEntity == null) {
            log.warn("召测指令未配置或指令ID错误！");
            return;
        }
        List<RelOrderStEntity> relOrderStEntityList = timeSynCallTaskMapper.relOrderStEntityList(TIME_SYN_ORDER_ID);
        if (relOrderStEntityList == null || relOrderStEntityList.size() < 1) {
            log.warn("无测站配置召测指令！");
            return;
        }
        List<RelOrderElementEntity> relOrderElementEntityList = timeSynCallTaskMapper.relOrderElementEntityList(TIME_SYN_ORDER_ID);
        if (relOrderElementEntityList == null || relOrderElementEntityList.size() < 1) {
            log.warn("召测指令未配置指令要素！");
            return;
        }
        orderBaseEntity.setOrderStEntityList(relOrderStEntityList);
        orderBaseEntity.setElementEntityList(relOrderElementEntityList);
        List<String> msgList = new ArrayList<>(5);
//        String nowTime = DATE_TIME_FORMATTER.format(LocalDateTime.now());
        String nowTime = "yyMMddHHmmss";
        msgList.add("");
        msgList.add("");
        msgList.add("");
        msgList.add("");
        msgList.add(4, nowTime);
        for (RelOrderStEntity stEntity : relOrderStEntityList) {
            String stCode = stEntity.getStCode();
            QuerySetOrderEntity setOrderEntity = generateOrder.generate(msgList, stCode, orderBaseEntity);
            log.info("生成指令为：{}", setOrderEntity);
            if (setOrderEntity == null) {
                log.error("指令未配置或指令错误！");
            }
            String s = HttpUtils.sendPost(address + CALL_URL, setOrderEntity);
            AjaxResult result = JSONObject.parseObject(s, AjaxResult.class);
            if (HttpStatus.SUCCESS == (int)result.get(AjaxResult.CODE_TAG)) {
                OrderRecordEntity entity = new OrderRecordEntity();
                entity.setOrderId(TIME_SYN_ORDER_ID);
                entity.setStCode(stCode);
                entity.setOrderNum(0);
//                recordService.insert(entity);
            }
        }

    }
}
